package com.heatup.apt.api.controller;


import com.heatup.apt.api.service.CoachCustomerService;
import com.heatup.apt.common.constants.ErrorCodeConstants;
import com.heatup.apt.common.util.JsonTools;
import com.heatup.apt.common.util.JsonUtils;
import com.heatup.apt.common.util.RequestTools;
import com.heatup.apt.common.util.ResponseTools;
import com.heatup.apt.model.api.ApiResultModel;
import com.heatup.apt.model.coach.CoachInfo;
import com.heatup.apt.model.user.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 *@Author: timo
 *@Date: 2018/3/22 10:32
 *@Description:
 * 教练端 - 客户管理控制器
 *
 */
@Controller
@RequestMapping("/coach/customer")
public class CoachCustomerController extends BaseController{

    Logger logger = LoggerFactory.getLogger(CoachCustomerController.class);

    @Resource
    private CoachCustomerService coachCustomerService;

    /**
     *@Author: timo
     *@Date: 2018/3/22 10:43
     *@Description:
     * 获取教练端-客户管理 的体测体验 私教课程列表
     * type 1 体测 体验 课程列表 体测体验的课程是未添加到日程管理列表的课程
     * type 2 私教课程列表 未完全销课的课程
     *
     */
    @ResponseBody
    @RequestMapping("/get_coach_customer_list")
    public String getCoachClientList(HttpServletRequest request){
        UserInfo userInfo = this.getUserInfo();
        String callback = RequestTools.RequestString(request,"callback","");
        ApiResultModel<Object> model = new ApiResultModel<>();
        Integer type = RequestTools.RequestInt(request,"type",1);
        Integer sonCoachId = RequestTools.RequestInt(request,"sonCoachId",0); //
        try {

            if (userInfo == null) {
                model.setLogined(this.isLogined());
                model.setRet(model.RET_ERROR);
                model.setMsg(ErrorCodeConstants.Code.USER_NOT_FIND.getMessage());
                model.setErrorCode(ErrorCodeConstants.Code.USER_NOT_FIND.getCode());
                return ResponseTools.CallBackResultJsonP(JsonUtils.obj2Json(model), callback);
            }
            CoachInfo coachInfo = getCoachInfo();
            if (coachInfo == null){
                model.setLogined(this.isLogined());
                model.setRet(model.RET_ERROR);
                model.setMsg(ErrorCodeConstants.Code.COACH_NOT_MATCH.getMessage());
                model.setErrorCode(ErrorCodeConstants.Code.COACH_NOT_MATCH.getCode());
                return ResponseTools.CallBackResultJsonP(JsonUtils.obj2Json(model), callback);
            }

            if (coachInfo.getIsPrivate() != 1){
                model.setLogined(this.isLogined());
                model.setRet(model.RET_ERROR);
                model.setMsg("温馨提示：您没有权限查看客户信息");
                return ResponseTools.CallBackResultJsonP(JsonUtils.obj2Json(model), callback);
            }

            if(sonCoachId > 0){
                if (coachInfo.getIsChiefCoach() != 1){
                    model.setLogined(this.isLogined());
                    model.setRet(model.RET_ERROR);
                    model.setMsg(ErrorCodeConstants.Code.NOT_HAS_PERMISSIONS.getMessage());
                    model.setErrorCode(ErrorCodeConstants.Code.NOT_HAS_PERMISSIONS.getCode());
                    return ResponseTools.CallBackResultJsonP(JsonUtils.obj2Json(model), callback);
                }
            }

            Map<String,Object> paramMap = new HashMap<>();
            paramMap.put("userInfo",userInfo);
            paramMap.put("type",type);
            paramMap.put("sonCoachId",sonCoachId);
            paramMap.put("isChiefCoach",coachInfo.getIsChiefCoach());
            model = coachCustomerService.queryCoachCustList(paramMap);
        }catch (Exception e){
            logger.error("getCoachCourseList | error:"+e.toString()+e.getCause(),e);
            model.setErrorCode(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
            model.setMsg(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage());
            model.setRet(model.RET_ERROR);
            model.setData(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage());
        }
        return ResponseTools.CallBackResultJsonP(JsonUtils.obj2Json(model), callback);
    }

    /**
     *@Author: timo
     *@Date: 2018/3/29 22:49
     *@Description:
     * 取消预约
     * 这里的取消订单表示被动取消 因为教练也是算是管理人员
     * 具体退款的话还是需要到管理后台中对应的订单列表去退款
     *
     */
    @ResponseBody
    @RequestMapping("/cancel_order")
    public String cancelOrder(HttpServletRequest request){
        ApiResultModel<Object> model = new ApiResultModel<>();
        UserInfo userInfo = this.getUserInfo();
        String callback = RequestTools.RequestString(request,"callback","");
        Integer orderId = RequestTools.RequestInt(request,"orderId",0);
        if (userInfo == null){
            model.setRet(model.RET_ERROR);
            model.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
            model.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
            return ResponseTools.CallBackResultJsonP(JsonUtils.obj2Json(model), callback);
        }

        try {
            model = coachCustomerService.coachCancelOrder(userInfo,orderId);
        }catch (Exception e){
            logger.error("cancelOrder | error:"+e.toString()+e.getCause(),e);
            model.setErrorCode(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
            model.setMsg(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage());
            model.setRet(model.RET_ERROR);
            model.setData(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage());
        }
        return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(model),callback);
    }
    // 查看订单详情

    public String getOrderDetail(Integer orderId, HttpServletRequest request){
        ApiResultModel<Object> model = new ApiResultModel<>();
        UserInfo userInfo = this.getUserInfo();
        String callback = RequestTools.RequestString(request,"callback","");

        try {
            if (userInfo == null) {
                model.setRet(model.RET_ERROR);
                model.setLogined(this.isLogined());
                model.setMsg(ErrorCodeConstants.Code.USER_NOT_FIND.getMessage());
                model.setErrorCode(ErrorCodeConstants.Code.USER_NOT_FIND.getCode());
                return ResponseTools.CallBackResultJsonP(JsonUtils.obj2Json(model), callback);
            }

        }catch (Exception e){
            logger.error("getOrderDetail | error:"+e.toString()+e.getCause(),e);
            model.setErrorCode(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
            model.setMsg(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage());
            model.setRet(model.RET_ERROR);
            model.setData(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage());
        }

        return ResponseTools.CallBackResultJsonP(JsonUtils.obj2Json(model), callback);

    }

    /**
     *@Author: timo
     *@Date: 2018/3/28 14:40
     *@Description:
     * 修改教练
     *
     */
    @ResponseBody
    @RequestMapping("/update_coach")
    public String updateCoach(HttpServletRequest request, @RequestBody Map<String,Object> paramMap){
        UserInfo userInfo = this.getUserInfo();
        String callback = RequestTools.RequestString(request,"callback","");
        ApiResultModel<Object> model = new ApiResultModel<>();
        Integer sonCoachId = RequestTools.RequestInt(request,"sonCoachId",0); //
        try {
            if (userInfo == null){
                model.setRet(model.RET_ERROR);
                model.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
                model.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
                return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(model),callback);
            }
            CoachInfo coachInfo = getCoachInfo();
            if(coachInfo == null){
                model.setRet(model.RET_ERROR);
                model.setMsg(ErrorCodeConstants.Code.COACH_NOT_MATCH.getMessage());
                model.setErrorCode(ErrorCodeConstants.Code.COACH_NOT_MATCH.getCode());
                return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(model),callback);
            }
            //
            if (sonCoachId > 0){
                if(coachInfo.getIsChiefCoach() != 1){
                    model.setRet(model.RET_ERROR);
                    model.setMsg(ErrorCodeConstants.Code.NOT_HAS_PERMISSIONS.getMessage());
                    model.setErrorCode(ErrorCodeConstants.Code.NOT_HAS_PERMISSIONS.getCode());
                    return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(model),callback);
                }
            }
            paramMap.put("userInfo",userInfo);
            paramMap.put("sonCoachId",sonCoachId);
            model = coachCustomerService.updateCoach(paramMap);
        }catch (Exception e){
            logger.error("updateCoach | error:"+e.toString()+e.getCause(),e);
            model.setErrorCode(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
            model.setMsg(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage());
            model.setRet(model.RET_ERROR);
            model.setData(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage());
        }
        model.setLogined(this.isLogined());
        return ResponseTools.CallBackResultJsonP(JsonUtils.obj2Json(model), callback);
    }

    /**
     *@Author: timo
     *@Date: 2018/3/28 17:35
     *@Description:
     * 查询订单教练团队列表
     *
     */
    @ResponseBody
    @RequestMapping("/get_order_coach_list")
    public String getOrderCoachList(HttpServletRequest request, Integer orderId){
        UserInfo userInfo = this.getUserInfo();
        String callback = RequestTools.RequestString(request,"callback","");
        ApiResultModel<Object> model = new ApiResultModel<>();
        try {
            model.setData(coachCustomerService.queryOrderCoachList(userInfo,orderId));
            model.setRet(model.RET_SUCCESS);
        }catch (Exception e){
            logger.error("getOrderCoachList | error:"+e.toString()+e.getCause(),e);
            model.setErrorCode(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
            model.setMsg(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage());
            model.setRet(model.RET_ERROR);
            model.setData(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage());
        }
        model.setLogined(this.isLogined());
        return ResponseTools.CallBackResultJsonP(JsonUtils.obj2Json(model), callback);
    }
}
